package com.xjh.him.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.layout.adapter.SmartViewHolder;
import com.xjh.him.R;
import com.xjh.him.dto.friendapply.FriendApplyDTO;
import com.xjh.him.dto.friendapply.FriendHandleApplyDTO;
import com.xjh.him.dto.user.UserDTO;
import com.xjh.him.utils.Utils;
import com.xjh.him.utils.XToastUtils;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView;

import java.util.List;

public class FriendApplyAdapter extends RecyclerView.Adapter<SmartViewHolder>{
    private Activity mActivity;
    private UserDTO userDTO;
    private List<FriendApplyDTO> dtoList;

    public FriendApplyAdapter(Activity activity, UserDTO userDTO, List<FriendApplyDTO> data) {
        mActivity = activity;
        this.userDTO = userDTO;
        dtoList = data;
    }

    @NonNull
    @Override
    public SmartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_apply_item, parent, false);
        return new SmartViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SmartViewHolder holder, int position) {
        SuperTextView stvApplyInfo = holder.findViewById(R.id.stv_apply_info);
        FriendApplyDTO applyDTO = dtoList.get(position);

        if (applyDTO.getApplyUserId().equals(userDTO.getId())) {
            ImageView leftIconIV = stvApplyInfo.getLeftIconIV();
            Utils.setImageViewParams(leftIconIV, 50, 50);
            Glide.with(mActivity)
                    .load(applyDTO.getAppliedUserAvatar())
                    .apply(Utils.getRoundedCornersRequestOptions(5))
                    .into(leftIconIV);
            stvApplyInfo.setLeftTopString(applyDTO.getAppliedUserNickname());
            stvApplyInfo.setLeftBottomString(applyDTO.getApplyInfo());
            if (FriendApplyDTO.STATE_APPLY.equals(applyDTO.getState())) {
                stvApplyInfo.setRightString("等待验证");
            }else if (FriendApplyDTO.STATE_ACCEPT.equals(applyDTO.getState())) {
                stvApplyInfo.setRightString("已添加");
            }
        }else {
            ImageView leftIconIV = stvApplyInfo.getLeftIconIV();
            Utils.setImageViewParams(leftIconIV, 50, 50);
            Glide.with(mActivity)
                    .load(applyDTO.getApplyUserAvatar())
                    .apply(Utils.getRoundedCornersRequestOptions(5))
                    .into(leftIconIV);
            stvApplyInfo.setLeftTopString(applyDTO.getApplyUserNickname());
            stvApplyInfo.setLeftBottomString(applyDTO.getApplyInfo());
            if (FriendApplyDTO.STATE_APPLY.equals(applyDTO.getState())) {
                stvApplyInfo.setRightString("接受");
            }else if (FriendApplyDTO.STATE_ACCEPT.equals(applyDTO.getState())) {
                stvApplyInfo.setRightString("已添加");
            }
            stvApplyInfo.getRightTextView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String handleApplyUrl = Utils.getSevServerApiUrl(mActivity, mActivity.getString(R.string.api_friend_apply_handle_apply));
                    FriendHandleApplyDTO handleApplyDTO = new FriendHandleApplyDTO();
                    handleApplyDTO.setId(applyDTO.getId());
                    handleApplyDTO.setHandleState(FriendApplyDTO.STATE_ACCEPT);
                    SimpleCallBack<Boolean> handleApplyCB = new SimpleCallBack<Boolean>() {
                        @Override
                        public void onSuccess(Boolean response) throws Throwable {
                            if (response) {
                                stvApplyInfo.setRightString("已同意");
                            }
                        }

                        @Override
                        public void onError(ApiException e) {
                            XToastUtils.toast(e.getDisplayMessage());
                        }
                    };
                    Utils.doPost(handleApplyUrl, JSON.toJSONString(handleApplyDTO), handleApplyCB);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return dtoList.size();
    }
}
